use crate::storage::{Storage, UpdateTodoItem};
use crate::ExecuteCommand;
use anyhow::Result;
use chrono::NaiveDate;
use clap::Parser;

#[derive(Parser, Debug)]
pub struct Edit {
    /// 要编辑的任务的 ID
    pub id: u32,

    /// 待办事项的内容（可选）
    pub content: Option<String>,

    /// 将待办设置为最高优先级（可选）
    #[arg(short = 't', long = "top", default_value_t = true)]
    pub top_priority: bool,

    /// 调整待办的日期（可选）
    #[arg(short,long,value_parser=crate::commands::parse_date)]
    pub date: Option<NaiveDate>,

    /// 修改后展示今天的待办
    #[arg(short = 'l', long = "list")]
    pub show_list: bool,
}

impl ExecuteCommand for Edit {
    fn execute_command(&self, storage: &impl Storage) -> Result<()> {
        storage.update(&self.into())?;

        if self.show_list {
            let list = crate::commands::list::List {
                days: None,
                status: None,
            };
            list.execute_command(storage)?;
        }

        Ok(())
    }
}

impl From<&Edit> for UpdateTodoItem {
    fn from(value: &Edit) -> Self {
        Self {
            id: value.id,
            content: value.content.clone(),
            top_priority: if value.top_priority { Some(true) } else { None },
            date: value.date,
        }
    }
}
